<?php

function parse_data($data,$p1,$p2){
	$data=" ".$data;
	$hasil="";
	$awal=strpos($data,$p1);
	if($awal!=""){
		$akhir=strpos(strstr($data,$p1),$p2);
		if($akhir!=""){
			$hasil=substr($data,$awal+strlen($p1),$akhir-strlen($p1));
		}
	}
	return $hasil;	
}

function get_mesin_id_by_host($host){
	$reult = my_query("SELECT id FROM a_mesin_lokasi WHERE ip_lokasi = '{$host}' LIMIT 1");
	$row = my_fetch_array($result);
	return $row['id'];
}
   
function get_karyawan_id_by_pin($pin){
	$reult = my_query("SELECT karyawan_id FROM a_mesin  WHERE finger_id = '{$pin}' LIMIT 1");
	$row = my_fetch_array($reult);
	return $row['karyawan_id'];
}

function save_data_download($mesin_lokasi_id , $host , $key){
 
	  
	$parseContent = connection_download($host , $key); 
		 
	$buffer=parse_data($parseContent,"<GetAttLogResponse>","</GetAttLogResponse>"); 
	$buffer=explode("\n",$buffer);
	$recs =0;
	 
	for($a=0;$a<count($buffer);$a++){
		$data	= 	Parse_Data($buffer[$a],"<Row>","</Row>");
		$PIN	=	Parse_Data($data,"<PIN>","</PIN>");
		$DateTime = Parse_Data($data,"<DateTime>","</DateTime>");
		//$Verified =	Parse_Data($data,"<Verified>","</Verified>");
		//$Status = Parse_Data($data,"<Status>","</Status>");
		$karyawan_id = get_karyawan_id_by_pin($PIN);
		if((int) $karyawan_id > 0){
			if(! check_data_exists($karyawan_id , $DateTime )){
				record_each_data($mesin_lokasi_id , $karyawan_id , $DateTime);
				$recs++;
			}
		}
	} 
	return $recs;
}

function record_each_data($mesin_lokasi_id , $karyawan_mesin_id , $datetime_log){
	$datas = array(
		'karyawan_mesin_id'	=> my_type_data_int($karyawan_mesin_id),
		'mesin_lokasi_id'	=> my_type_data_int($mesin_lokasi_id ),
		'datetime_mesin_log'=> my_type_data_str($datetime_log),
		'datetime_added'	=> my_type_data_function('NOW()'),
	);
	return my_insert_record('a_mesin_capture',$datas);
}

function check_data_exists($karyawan_mesin_id , $datetime){
	$query = "SELECT * FROM a_mesin_capture WHERE  karyawan_mesin_id = {$karyawan_mesin_id} AND datetime_mesin_log = '{$datetime}' ";
	$result = my_query($query);
	if( my_num_rows($result) > 0){
		return true;
	}
	return false;
}
 
function connection_download($host , $Key  ){
	$Connect = fsockopen($host, "80", $errno, $errstr, 1);
 
	if($Connect){
		$soap_request="<GetAttLog><ArgComKey xsi:type=\"xsd:integer\">".$Key."</ArgComKey><Arg><PIN xsi:type=\"xsd:integer\">All</PIN></Arg></GetAttLog>";
		$newLine="\r\n";
		fputs($Connect, "POST /iWsService HTTP/1.0".$newLine);
		fputs($Connect, "Content-Type: text/xml".$newLine);
		fputs($Connect, "Content-Length: ".strlen($soap_request).$newLine.$newLine);
		fputs($Connect, $soap_request.$newLine);
		$buffer="";
		while($Response=fgets($Connect, 1024)){
			$buffer=$buffer.$Response;
		} 
		return $buffer;
	}
	return false;
}

function display_48_hour_later(){
	
	$header = array( 
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'tanggal'=>array('style'=>'border-bottom:2px solid;width:20%'), 		
		'Waktu'=>array('style'=>'border-bottom:2px solid;width:15%'), 		 
		//'IN'=>array('style'=>'border-bottom:2px solid;width:5%'), 		 
	//	'OUT'=>array('style'=>'border-bottom:2px solid;width:5%'), 		 
		//' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key'])){
		$query = "SELECT * 
			FROM a_mesin_capture a
			INNER JOIN karyawan b ON b.karyawan_id = a.karyawan_mesin_id  
			INNER JOIN biodata c ON b.biodata_id = c.biodata_id 
			WHERE a.datetime_mesin_log >= DATE( NOW( ) ) + INTERVAL HOUR( NOW( ) ) - 48 HOUR 
			AND a.datetime_mesin_log <= DATE( NOW( ) ) + INTERVAL HOUR( NOW( ) ) HOUR 
			AND b.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND b.karyawan_id NOT IN ( select karyawan_id FROM pemberhentian ) 	
			AND ( c.nama LIKE '%{$_GET['key']}%' OR b.nik ='{$_GET['key']}' )
			ORDER BY a.karyawan_mesin_id ASC , a.datetime_mesin_log ASC

		";
	}else{
		$query = "SELECT * 
			FROM a_mesin_capture
			WHERE datetime_mesin_log >= DATE( NOW( ) ) + INTERVAL HOUR( NOW( ) ) - 48 HOUR 
			AND datetime_mesin_log <= DATE( NOW( ) ) + INTERVAL HOUR( NOW( ) ) HOUR 
			ORDER BY  karyawan_mesin_id ASC ,  datetime_mesin_log ASC
		";
	} 
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =30;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 
	$row = array(); 
	$i = ($current_page  - 1 ) * 15; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_mesin_id']);   
		 $detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=detail&id='.$ey['capture_id'],
			'title'=>'Detail','rel'=>'facebox'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$row[] = array( 
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=> $karyawan['nama_gelar'],    	
			'tanggal'=>date('d-m-Y',strtotime($ey['datetime_mesin_log'])),			 	 	 	 	 	 	
			'time'=>date('H:i:s',strtotime($ey['datetime_mesin_log'])) , 
		//	'in'=>'', 
		//	'out'=>'', 
		//	'operasi'=> position_text_align( $detail_button  , 'right'),  
		);
		
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(  
		//'Create' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=create\' });'),
		//'Update Finger'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=download\';'),
	);	
	
	$printmenu = additional_menu_on_list( $menuontop, $search);
	return $printmenu .table_builder($header , $datas ,  15 ,false , $paging  );
}


?>	 